#define _CRT_SECURE_NO_WARNINGS
// dfs
class Solution {
public:
    int countNodes(TreeNode* root) {
        return dfs(root);
    }
    int dfs(TreeNode* root) {
        if (root == nullptr) return 0;
        int left = dfs(root->left);
        int right = dfs(root->right);
        return left + right + 1;
    }
};
// bfs
class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> q;
        if (root != nullptr) q.push(root);
        int count = 0;
        while (!q.empty())
        {
            int size = q.size();
            for (int i = 0; i < size; i++)
            {
                TreeNode* top = q.front();
                q.pop();
                count++;
                if (top->left) q.push(top->left);
                if (top->right) q.push(top->right);
            }
        }
        return count;
    }
};